//
//  AddressManagerCell.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/8.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "AddressManagerCell.h"
#import "AddressModel.h"

@implementation AddressManagerCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)setModel:(AddressModel *)model{
    _model = model;
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.textColor = COLOR_HEX(0x121212);
    nameLabel.text = _model.collectName;
    [self.contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(10));
        make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(150), scaleTopAndBottom(20)));
    }];
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.textAlignment = NSTextAlignmentRight;
    phoneLabel.font = [UIFont systemFontOfSize:14];
    phoneLabel.textColor = COLOR_HEX(0x121212);
    phoneLabel.text = _model.phoneNumber;
    [self.contentView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(150), scaleTopAndBottom(20)));
    }];
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [editBtn setTitleColor:COLOR_HEX(0x121212) forState:UIControlStateNormal];
    editBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [editBtn setTitle:@" 编辑" forState:UIControlStateNormal];
    [editBtn setImage:[UIImage imageNamed:@"图层-7"] forState:UIControlStateNormal];
    [self.contentView addSubview:editBtn];
    _delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_delBtn setTitle:@" 删除" forState:UIControlStateNormal];
    _delBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_delBtn setTitleColor:COLOR_HEX(0x121212) forState:UIControlStateNormal];
    [_delBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [editBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_delBtn setImage:[UIImage imageNamed:@"图层-8"] forState:UIControlStateNormal];
    [self.contentView addSubview:_delBtn];
    [_delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-10));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.width.mas_equalTo(scaleLeftAndRight(70));
    }];
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_delBtn.mas_left).offset(scaleLeftAndRight(-10));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.width.mas_equalTo(scaleLeftAndRight(70));
    }];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.bottom.equalTo(editBtn.mas_top).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(0.5);
    }];
    UILabel *addressLabel = [[UILabel alloc] init];
    addressLabel.font = [UIFont systemFontOfSize:14];
    addressLabel.numberOfLines = 0;
    addressLabel.textColor = COLOR_HEX(0x333333);
    addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",_model.province,_model.city,_model.area,_model.address];
    addressLabel.numberOfLines = 0;
    [self.contentView addSubview:addressLabel];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(nameLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(10));
        make.bottom.equalTo(lineView.mas_top).offset(scaleTopAndBottom(-5));
    }];
    self.defalutBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.contentView addSubview:_defalutBtn];
    if ([model.isDefault isEqual:@"true"]) {
        self.defalutBtn.selected = YES;
        NSLog(@"是吗");
    }
    self.addressId = model.Id;
    [_defalutBtn setTitle:@" 设为默认" forState:UIControlStateNormal];
    [_defalutBtn setTitle:@" 默认地址" forState:UIControlStateSelected];
    [_defalutBtn addTarget:self action:@selector(chooseOrNorChooseDefault:) forControlEvents:UIControlEventTouchUpInside];
    [_defalutBtn setTitleColor:COLOR_HEX(0x121212) forState:UIControlStateNormal];
    _defalutBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_defalutBtn setImage:[UIImage imageNamed:@"defultAdress"] forState:UIControlStateNormal];
        [_defalutBtn setImage:[UIImage imageNamed:@"selectDefultAddress"] forState:UIControlStateSelected];
    _defalutBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_defalutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.width.mas_equalTo(scaleLeftAndRight(100));
        make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(-2));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
    }];
    
}

-(void)chooseOrNorChooseDefault:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.defalutBlock(self.addressId);
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
